import { login, logout, reLoginData } from '@/api/login'
import { getToken,
  setToken,
  removeToken,
  setCookie,
  removeCookie,
  getReToken,
  setReToken,
  removeReToken } from '@/utils/auth'
import { compare ,compareASC} from '@/utils/index'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    id: '',
    roles: [],
    menus: [],
    btns: '',
    uInfo: {},
    re_token: getReToken()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_BTN: (state, btns) => {
      state.btns = btns
    },
    SET_INFO: (state, info) => {
      state.uInfo = info
    },
    SET_RETOKEN: (state, re_token) => {
      state.re_token = re_token
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
       const data = {
        access_token: 'token',
        refresh_token: 'refresh_token'
      }
      setToken(data.access_token)
      setReToken(data.refresh_token)
      commit('SET_TOKEN', data.access_token)
      commit('SET_RETOKEN', data.refresh_token)
      return
      const username = encodeURIComponent(userInfo.username.trim())
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response
          setToken(data.access_token)
          setReToken(data.refresh_token)
          commit('SET_TOKEN', data.access_token)
          commit('SET_RETOKEN', data.refresh_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getInfo({ commit, state }, id) {
      return new Promise(resolve => {
        const roles = ['admin','edit']
        commit('SET_ROLES',roles )
        commit('SET_NAME', "luolinlan")
        commit('SET_AVATAR', "")
        resolve(roles)
      })
    },
    // 重新获取token
    reLogin({ commit }, re_token) {
      return new Promise((resolve, reject) => {
        reLoginData(re_token).then(response => {
          const data = response
          setToken(data.access_token)
          setReToken(data.refresh_token)
          commit('SET_TOKEN', data.access_token)
          commit('SET_RETOKEN', data.refresh_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 重新设置用户信息
    reSetInfo({ commit, state }, data) {
      return new Promise(resolve => {
        commit('SET_ID', data.userModel.id)
        commit('SET_INFO', data)
        resolve()
      })
    },
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          removeCookie('uidDEV')
          removeCookie('unameDEV')
          removeCookie('btnsDEV')
          removeCookie('menusDEV')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_RETOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_INFO', {})
        removeToken()
        removeReToken()
        removeCookie('uidDEV')
        /* removeCookie("toPathMeetSys")*/
        localStorage.clear()
        resolve()
      })
    },
    // 重新设置token
    reSetToken({ commit, state }, token) {
      return new Promise(resolve => {
        setToken(token)
        commit('SET_TOKEN', token)
        resolve()
      })
    },
    // 登录时设置token值
    SetTokeName({ commit, state }) {
      return new Promise(resolve => {
        setToken('Basic YnJvd3Nlcjo=')
        commit('SET_TOKEN', 'Basic YnJvd3Nlcjo=')
        resolve()
      })
    }
  }
}

export default user
